package io.geekidea.boot.shop.controller.web;

import com.alibaba.fastjson2.JSON;
import io.geekidea.boot.auth.annotation.Permission;
import io.geekidea.boot.common.enums.SysLogType;
import io.geekidea.boot.framework.annotation.Log;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.shop.dto.MemberDto;
import io.geekidea.boot.shop.query.CustomerQuery;
import io.geekidea.boot.shop.query.web.MemberQuery;
import io.geekidea.boot.shop.service.base.api.MemberService;
import io.geekidea.boot.shop.service.business.api.ICustomerService;
import io.geekidea.boot.shop.vo.CustomerLevelVo;
import io.geekidea.boot.shop.vo.web.MemberVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 用户信息 控制器
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@RestController
@Tag(name = "用户信息")
@RequestMapping("/admin/member")
public class MemberController {

    @Resource
    private MemberService memberService;

    @Resource
    private ICustomerService customerService;

    @Log(type = SysLogType.ALL_LIST)
    @Operation(summary = "查询用户信息")
    @PostMapping("/queryMember")
    @Permission("member:add")
    public ApiResult<Paging<CustomerLevelVo>> queryMember(@Valid @RequestBody CustomerQuery customerDto) {
        log.info("查询客户信息：{}", JSON.toJSONString(customerDto));
        Paging<CustomerLevelVo> customerLevelInfo = customerService.getCustomerLevelInfo(customerDto);
        return ApiResult.success(customerLevelInfo);
    }

    @Log(type = SysLogType.ALL_LIST)
    @Operation(summary = "查询推荐人列表")
    @PostMapping("/queryInvite")
    @Permission("member:add")
    public ApiResult<Paging<CustomerLevelVo>> queryInvite(@RequestBody CustomerQuery customerDto) {
        log.info("查推荐关系：{}", customerDto);

        Paging<CustomerLevelVo> customerLevelInfo = customerService.getInviteList(customerDto);
        return ApiResult.success(customerLevelInfo);
    }

    /**
     * 添加用户信息
     *
     */
    @Log(type = SysLogType.ADD)
    @Operation(summary = "添加用户信息")
    @PostMapping("/addMember")
    @Permission("member:add")
    public ApiResult addMember(@Valid @RequestBody MemberDto dto) {
        log.info("添加用户信息：{}", dto);
        boolean flag = memberService.addMember(dto);
        return ApiResult.result(flag);
    }

    /**
     * 修改用户信息
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.UPDATE)
    @Operation(summary = "修改用户信息")
    @PostMapping("/updateMember")
    @Permission("member:update")
    public ApiResult updateMember(@Valid @RequestBody MemberDto dto) {
        log.info("修改用户信息：{}", dto);
        boolean flag = memberService.updateMember(dto);
        return ApiResult.result(flag);
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.DELETE)
    @Operation(summary = "删除用户信息")
    @PostMapping("/deleteMember/{id}")
    @Permission("member:delete")
    public ApiResult deleteMember(@PathVariable String id) {
        log.info("删除用户信息：{}", id);
        boolean flag = memberService.deleteMember(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取用户信息详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取用户信息详情")
    @PostMapping("/getMember/{id}")
    @Permission("member:info")
    public ApiResult<MemberVo> getMember(@PathVariable String id) {
        log.info("获取用户信息详情：{}", id);
        MemberVo memberVo = memberService.getMemberById(id);
        return ApiResult.success(memberVo);
    }

    /**
     * 获取用户信息分页列表
     *
     * @param query
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取用户信息分页列表")
    @PostMapping("/getMemberPage")
    @Permission("member:page")
    public ApiResult<MemberVo> getMemberPage(@Valid @RequestBody MemberQuery query) {
        log.info("获取用户信息分页列表：{}", query);
        Paging<MemberVo> paging = memberService.getMemberPage(query);
        return ApiResult.success(paging);
    }

}
